package com.example.final_project.controller;

import com.example.final_project.entity.Inventory;
import com.example.final_project.service.InventoryService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "库存管理", description = "库存相关的API接口")
@Controller
@RequestMapping("/inventory")
public class InventoryController {

    @Autowired
    private InventoryService inventoryService;

    @Operation(summary = "库存列表页面", description = "返回库存列表页面")
    @GetMapping("/list")
    public String list(Model model,
                       @Parameter(description = "教材名称") @RequestParam(required = false) String bookName,
                       @Parameter(description = "ISBN") @RequestParam(required = false) String isbn,
                       @Parameter(description = "作者") @RequestParam(required = false) String author,
                       @Parameter(description = "出版社ID") @RequestParam(required = false) Long publisherId,
                       @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
                       @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Inventory> pageInfo = inventoryService.getList(bookName, isbn, author, publisherId, pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("bookName", bookName);
        model.addAttribute("isbn", isbn);
        model.addAttribute("author", author);
        model.addAttribute("publisherId", publisherId);
        return "inventory/list";
    }

    @Operation(summary = "获取库存列表API", description = "分页获取库存列表数据")
    @ApiResponse(responseCode = "200", description = "成功获取库存列表")
    @GetMapping("/api/list")
    @ResponseBody
    public Map<String, Object> apiList(
            @Parameter(description = "教材名称") @RequestParam(required = false) String bookName,
            @Parameter(description = "ISBN") @RequestParam(required = false) String isbn,
            @Parameter(description = "作者") @RequestParam(required = false) String author,
            @Parameter(description = "出版社ID") @RequestParam(required = false) Long publisherId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize) {
        
        PageInfo<Inventory> pageInfo = inventoryService.getList(bookName, isbn, author, publisherId, pageNum, pageSize);
        
        Map<String, Object> result = new HashMap<>();
        result.put("list", pageInfo.getList());
        result.put("total", pageInfo.getTotal());
        return result;
    }

    @Operation(summary = "更新库存", description = "更新库存数量")
    @ApiResponse(responseCode = "200", description = "更新成功返回true，失败返回false")
    @PostMapping("/api/update")
    @ResponseBody
    public boolean update(@Parameter(description = "库存信息") @RequestBody Inventory inventory) {
        return inventoryService.update(inventory);
    }

    @Operation(summary = "根据ID获取库存", description = "根据库存ID获取库存详细信息")
    @ApiResponse(responseCode = "200", description = "成功获取库存信息")
    @GetMapping("/api/{id}")
    @ResponseBody
    public Inventory getById(@Parameter(description = "库存ID") @PathVariable Long id) {
        return inventoryService.getById(id);
    }

    @Operation(summary = "根据教材ID获取库存", description = "根据教材ID获取库存详细信息")
    @ApiResponse(responseCode = "200", description = "成功获取库存信息")
    @GetMapping("/api/book/{bookId}")
    @ResponseBody
    public Inventory getByBookId(@Parameter(description = "教材ID") @PathVariable Long bookId) {
        return inventoryService.getByBookId(bookId);
    }
} 